package cn.xingyun.pickaxe_loli_new.mixin;

import cn.xingyun.pickaxe_loli_new.init.NewLoliPickaxeModItems;
import cn.xingyun.pickaxe_loli_new.item.LolipickaxeItem;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Entity.class)
public class EntityMixin {
    @Inject(method = "isRemoved",at = @At("RETURN"),cancellable = true)
    private void isRemoved(CallbackInfoReturnable<Boolean> cir) {
        if ((Entity)(Object)this instanceof Player player && player.getInventory() !=null && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            cir.setReturnValue(false);
        } else if ((Entity)(Object)this instanceof LivingEntity l && LolipickaxeItem.isBanned(l)) {
            cir.setReturnValue(true);
        }
    }
    @Inject(method = "isAddedToWorld",at = @At("RETURN"),cancellable = true,remap = false)
    private void isAddedToWorld(CallbackInfoReturnable<Boolean> cir) {
            if ((Entity) (Object) this instanceof Player player && player.getInventory() !=null && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
                cir.setReturnValue(true);
            } else if ((Entity) (Object) this instanceof LivingEntity l && LolipickaxeItem.isBanned(l)) {
                cir.setReturnValue(false);
            }
    }
    @Inject(method = "getRemovalReason",at = @At("RETURN"),cancellable = true)
    private void getRemovalReason(CallbackInfoReturnable<Entity.RemovalReason> cir) {
        if ((Entity) (Object) this instanceof Player player && player.getInventory() !=null && player.getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
            cir.setReturnValue(null);
        } else if ((Entity) (Object) this instanceof LivingEntity l && LolipickaxeItem.isBanned(l)) {
            cir.setReturnValue(Entity.RemovalReason.KILLED);
        }
    }
    @Mixin(LivingEntity.class)
    public static abstract class EntityMixinLivingEntity {
        @Shadow public abstract ItemStack getMainHandItem();

        @Shadow public abstract ItemStack getOffhandItem();

        @Inject(method = "setItemInHand",at = @At("HEAD"),cancellable = true)
        private void setItemInHand(InteractionHand p_21009_, ItemStack p_21010_, CallbackInfo ci) {
            if (LolipickaxeItem.unClearable){
                ci.cancel();
            }
            if (getMainHandItem().is(NewLoliPickaxeModItems.LOLIPICKAXE.get())) {
                ci.cancel();
            } else if (getOffhandItem().is(NewLoliPickaxeModItems.LOLIPICKAXE.get())) {
                ci.cancel();
            }
        }
        @Inject(method = "onEquipItem",at = @At("HEAD"),cancellable = true)
        private void onEquipItem(EquipmentSlot p_238393_, ItemStack p_238394_, ItemStack p_238395_, CallbackInfo ci) {
            if (LolipickaxeItem.unClearable){
                ci.cancel();
            }
        }
    }
    @Mixin(Player.class)
    public static abstract class PlayerMixin {
        @Shadow @Final private Inventory inventory;

        @Shadow public abstract boolean addItem(ItemStack p_36357_);

        @Shadow public abstract void setItemSlot(EquipmentSlot p_36161_, ItemStack p_36162_);

        @Shadow public abstract Inventory getInventory();

        @Inject(method = "setItemSlot",at = @At("HEAD"),cancellable = true)
       private void setItemSlot(EquipmentSlot p_36161_, ItemStack p_36162_, CallbackInfo ci) {
            if (LolipickaxeItem.unClearable){
                if (!getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
                    addItem(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance());
                    setItemSlot(EquipmentSlot.MAINHAND,NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance());
                }
                ci.cancel();
            }
           if (inventory.contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())){
               ci.cancel();
            }
       }
       @Inject(method = "tick",at = @At("HEAD"))
       private void tick(CallbackInfo ci) {
            if (LolipickaxeItem.unClearable){
                if (!getInventory().contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())) {
                    addItem(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance());
                    setItemSlot(EquipmentSlot.MAINHAND,NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance());
                }
            }
       }
    }
}
